﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SiGeSIB.GUI.Common;
using SiGeSIB.Data.Management;
using SiGeSIB.Services;
using SiGeSIB.GUI.Common.DedaGrid.Utils;
using SiGeSIB.GUI.Common.DedaGrid.Filter;
using SiGeSIB.Data.Common;

namespace SiGeSIB.GUI.Management
{
#if DESIGN
	public partial class FManageMembers : FormRedirector.FManageMembers
#else
	public partial class FManageMembers : FGenericManager<Contributor>
#endif
	{
		#region ColumnsNames
		private const string DT_NAME = "Nome";
		private const string DT_PHONE = "Telefone";
		private const string DT_BORNDATE = "Data de nascimento";
		private const string DT_MEMBERSYTYPE = "Tipo de membresia";
		private const string DT_DISRUPTIONDATE = "Data de afastamento";
		private const string DT_DISRUPTIONTYPE = "Tipo de afastamento";
		private const string DT_CONTRIBUTORTYPE = "Tipo";
		#endregion

		#region Constructor(s)
		public FManageMembers(EContributorType type)
		{
			InitializeComponent();

			_Type = type;

			if (type == EContributorType.Contributor)
				this.Text = "Gerenciamento de contribuinte";
			else if (type == EContributorType.Visitor)
				this.Text = "Gerenciamento de visitante";
			else
				this.Text = "Gerenciamento de membro";
		}
		#endregion

		#region Attributes
		private EContributorType _Type;
		#endregion

		#region Properties
		public EContributorType Type { get { return _Type; } }

		protected override string _Description
		{
			get 
			{
				string desc = string.Empty;

				switch (_Type)
				{
					case EContributorType.Member:
						desc = "os Membros do sistema";
						break;
					case EContributorType.Visitor:
						desc = "os Visitantes do sistema";
						break;
					case EContributorType.Away:
						desc = "os Membros Afastados do sistema";
						break;
					case EContributorType.Contributor:
						desc = "os Contribuintes do sistema";
						break;
					default:
						break;
				}
				
				return desc;
			}
		}
		#endregion

		#region EventHandlers
		protected override void uManageDefaultToolStrip_AddClicked(object sender, EventArgs e)
		{
			Manager.Instance.CreateParentTab(this, new FEditMember(EFormState.Edit, Type, -1));
		}

		protected override void uManageDefaultToolStrip_EditClicked(object sender, EventArgs e)
		{
			DataRow row = uEntitiesGrid.GetSelectedRow();
			int currentId = (int)row[DT_ID];
			Manager.Instance.CreateParentTab(this, new FEditMember(EFormState.View, Type, currentId));
		}

		#endregion

		#region Methods
		#region Loading
		protected override void _InitializeDataColumns()
		{
			this._DedaColumns = new List<DedaGridColumn>()
			{
				new DedaGridColumn(DT_ID, typeof(int)) { Visible = false },
				new DedaGridColumn(DT_NAME, typeof(string)),
				new DedaGridColumn(DT_PHONE, typeof(string)),
				new DedaGridColumn(DT_BORNDATE, typeof(string)) { Visible = (Type & (EContributorType.Member | EContributorType.Visitor)) != 0},
				new DedaGridColumn(DT_MEMBERSYTYPE, typeof(string)){ Visible = (Type & EContributorType.Member) != 0},
				new DedaGridColumn(DT_DISRUPTIONDATE, typeof(string)) { Visible = (Type & EContributorType.Away) != 0},
				new DedaGridColumn(DT_DISRUPTIONTYPE, typeof(string)){ Visible = (Type & EContributorType.Away) != 0},
				new DedaGridColumn(DT_CONTRIBUTORTYPE, typeof(string)){ Visible = (Type & EContributorType.Contributor) != 0}
			};
		}

		protected override object[] _ConvertToDataRow(Contributor c)
		{
			return new object[]
				{
					c.Id,
					c.Name,
					c.Phone,
					c is Frequenter && (c as Frequenter).BirthdayDate.HasValue ? (c as Frequenter).BirthdayDate.Value.ToShortDateString() : null,
					c is Member ? ((c as Member).MembershipType.GetDescription()) : null,
					c is Member && (c as Member).DisruptionDate.HasValue ? (c as Member).DisruptionDate.Value.ToShortDateString() : null,
					c is Member ? (c as Member).DisruptionType.GetDescription() : null,
					c.Type.GetDescription()
				};
		} 

		protected override List<Contributor> _LoadEntity()
		{
			IManagementServicesProvider manProv = Manager.Instance.ServicesProvider.CreateManagementServicesProvider();

			List<Contributor> members = null;

			if (Type == EContributorType.Contributor)
				members = manProv.GetContributors(null);
			else
				members = manProv.GetContributors(Type);

			return members;
		}
		#endregion

		protected override bool _RequestRemove()
		{
			return MessageBox.Show(this, "Deseja remover o membro selecionado?", "Confirmação", MessageBoxButtons.OKCancel) == DialogResult.OK;
		}

		protected override void _ApplyFilter(string p)
		{
			DedaGridFilterCollection collection = new DedaGridFilterCollection();
			collection.Add(new DedaGrigFilterCondition(_DtTable.Columns[DT_NAME], ELogicalCondition.CONTAINS, p));
			collection.Add(new DedaGrigFilterCondition(EDedaGridFilterLogicalConnector.OR, _DtTable.Columns[DT_BORNDATE], ELogicalCondition.CONTAINS, p));
			collection.Add(new DedaGrigFilterCondition(EDedaGridFilterLogicalConnector.OR, _DtTable.Columns[DT_MEMBERSYTYPE], ELogicalCondition.CONTAINS, p));
			collection.Add(new DedaGrigFilterCondition(EDedaGridFilterLogicalConnector.OR, _DtTable.Columns[DT_PHONE], ELogicalCondition.CONTAINS, p));
			collection.Add(new DedaGrigFilterCondition(EDedaGridFilterLogicalConnector.OR, _DtTable.Columns[DT_DISRUPTIONDATE], ELogicalCondition.CONTAINS, p));
			collection.Add(new DedaGrigFilterCondition(EDedaGridFilterLogicalConnector.OR, _DtTable.Columns[DT_DISRUPTIONTYPE], ELogicalCondition.CONTAINS, p));
			collection.Add(new DedaGrigFilterCondition(EDedaGridFilterLogicalConnector.OR, _DtTable.Columns[DT_CONTRIBUTORTYPE], ELogicalCondition.CONTAINS, p));

			_DtTable.Filter(collection);
		}

		protected override void _RemoveEntity(int id)
		{
			IManagementServicesProvider manProv = Manager.Instance.ServicesProvider.CreateManagementServicesProvider();
			manProv.RemoveContributor(id);
		}
		#endregion
	}
}
